﻿var ue = UE.getEditor('content', {
    toolbars: [
        ['fullscreen', 'undo', 'redo', 'bold', 'emotion',
            'selectall', 'link', 'insertcode', 'fontfamily', 'fontsize']],
    enableAutoSave: false,
    maximumWords: 500,
    autoHeightEnabled: false,
    autoFloatEnabled: false,
    topOffset: 100,
    fontsize: UeditorFontSizeArray
});

var sendhtml = null;
function goSend() {
    if ($('#hCheck').length > 0) {
        if (!layerCheckLogin(true)) {
            return;
        }
    }
    viewshow(0, 0, null, null);
}

function viewshow(j, pid, name, type) {
    if (sendhtml == null || sendhtml == undefined) {
        sendhtml = $('#dvsendbugmain').html();
        $('#dvsendbugmain').empty();
    }
    var title = pid > 0 ? ('回复' + j + '楼：@' + name) : ('我要提交bug');
    //var h = $('#huid').length > 0 ? 370 : 470;
    layerOpen(title, sendhtml, 750, 470, function (id) {
        //ue.focus();//加上编辑器就会死掉我擦
        if ($('#txtvistorEmail').length > 0) {
            $('#txtvistorEmail').mailAutoComplete();
            $(".emailist").css({ marginTop: '0px', marginLeft: '90px' });
        }
        $("#hPID").val(pid);
        $('#btncancel').on("click", function () { layer.close(id); $('#dvsendbugmain').html(sendhtml); sendhtml = null; });
        if (pid > 0) {
            $('#bugType').val(type).attr("disabled", "disabled");
        }
        $('#btnok').on("click", function () {
            var vistor;
            var visuid;
            var visEmail;
            if ($('#txtvistor').length > 0 && !$.trim($('#txtvistor').val())) {
                layerAlert('敢问阁下是何方高人？', function () { $('#txtvistor').focus(); });
                return;
            } else {
                vistor = $('#txtvistor').val();
            }
            if ($('#txtvistorEmail').length > 0) {
                if (!$.trim($('#txtvistorEmail').val())) {
                    layerAlert('请留下您的Email，bug修复后我们必将告知与你！', function () { $('#txtvistorEmail').focus(); });
                    return;
                }
                if (!/[A-Za-z0-9._%+-]+@@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/.test($.trim($('#txtvistorEmail').val()))) {
                    layerAlert('阁下输入的Email格式不对哦！', function () { $('#txtvistorEmail').focus(); });
                    return;
                }
                visEmail = $('#txtvistorEmail').val();
            }
            if ($("#txtuid").length > 0) {
                visuid = $("#txtuid").val();
            }
            if (!$.trim($('#bugType').val())) {
                layerAlert('请选择bug类型！', function () { $('#bugType').focus(); });
                return;
            }
            if (!ue.hasContents()) {
                layerAlert($("#hPID").val() == "0" ? '请阁下描述一下bug的形成过程！' : '回复内容还没写哦！', function () { ue.focus(); });
                return;
            }
            if (ue.getContent().length < 5) {
                layerAlert($("#hPID").val() == "0" ? 'bug描述不能少于5个字哦！' : "回复内容不能少于5个字哦！", function () { ue.focus(); });
                return;
            }
            var url;
            var prm;
            if ($("#hPID").val() == "0") {
                url = "/system/sendbug";
                prm = { pid: 0, visitor: $.htmlEncode(vistor), uid: visuid, visEmail: visEmail, bugDetail: $.htmlEncode(ue.getContent()), bugtype: $('#bugType').val() };
            } else {
                url = "/system/ReplyBug";
                prm = { pid: $("#hPID").val(), content: $.htmlEncode('@' + name + ue.getContent()) }
            }
            $.post(url, prm, function (json) {
                if (json.State == 1) {
                    clear();
                    $(window).scrollTop(0);
                    loadBug(1);
                    if ($("#hPID").val() == "0") {
                        changeCount(1);
                        layer.msg('bug提交成功，谢谢高人指点！', { icon: 1 });
                    } else {
                        layer.msg('回复成功！', { icon: 1 });
                    }
                    $('#dvsendbugmain').html(sendhtml); sendhtml = null;
                    layer.close(id);
                } else {
                    layerAlert(json.Message);
                }
            }, "json");
        });

    });
}

$(function () {
    
    //$(window).scroll(function () {
    //    var deh = $("#dvsendbugmain").height();
    //    var topScroll = $(document).scrollTop();
    //    if (parseInt(topScroll) <= 70) {
    //        $("#dvsendbugmain").animate({ "top": topScroll == 0 ? 72 : (72 - topScroll) }, 20);
    //    } else {
    //        if (parseInt(topScroll) < parseInt(deh)) {
    //            topScroll = deh;
    //        }
    //        $("#dvsendbugmain").animate({ "top": topScroll + 72 }, 20);
    //    }
    //});
});

function clear() {
    if ($('#txtvistorEmail').length > 0) {
        $('#txtvistor').val('');
        $('#txtvistorEmail').val('');
    }
    ue.setContent('');
}

function loadBug(page) {
    $('#dvbuglist').load("/system/_SysBugManager", { page: page }, function () { doUeParse(); });
}

function changeCount(count) {
    $('#spcount').html(parseInt($('#spcount').html()) + count);
}

function flodReply(o) {
    if ($(o).text() == '折叠') {
        $(o).text('展开');
        $(o).parent().parent().nextAll('div').hide(500);
    } else {
        $(o).text('折叠');
        $(o).parent().parent().nextAll('div').show(500);
    }
}

function replyBug(j, pid, name, type) {
    if (!layerCheckLogin(true)) {
        return;
    }
    viewshow(j, pid, name, type);
}

function delBug(id) {
    layer.confirm('确定删除这个Bug吗？', {
        icon: 3,
        title: '提示',
        btn: ['确定', '不要'] //按钮
    }, function () {
        $.post("/system/DelBug", { id: id }, function (json) {
            if (json.State == 1) {
                layer.msg('删除成功！', { icon: 1 });
                loadBug(1);
                changeCount(-1);
            } else {
                layerAlert(json.Message);
            }
        }, "json");
    });
}

function delRpBug(id) {
    layer.confirm('确定删除这条回复吗？', {
        icon: 3,
        title: '提示',
        btn: ['确定', '不要'] //按钮
    }, function () {
        $.post("/system/DelRpBug", { id: id }, function (json) {
            if (json.State == 1) {
                layer.msg('删除成功！', { icon: 1 });
                loadBug(1);
            } else {
                layerAlert(json.Message);
            }
        }, "json");
    });
}

function showmore(o) {
    var index = layer.load();
    var h = $(o).offset().top;
    var curpage = parseInt($("#hidpage").val()) + 1;
    $.post("/system/_SysBugManager", { page: curpage }, function (html) {
        layerMsg("加载成功~", null, null, 2000);
        $(o).parent().remove();
        $('#dvbuglist').append(html);
        $("#hidpage").val(curpage);
        layer.close(index);
        $("html,body").animate({ scrollTop: (h - 25) + 'px' }, 1500);
        doUeParse();
    }, "html");
}

function agree(id, count, a) {
    doAgree('/system/AgreeBug', id, count, a);
}
